//提交信息-网银
var clickflag = true;

function submitbank() {
    var form = "#pay-ebank";
    if (clickflag == false) {
        // return false;
    }
    clickflag = false;
    var acctype = $(form + " #acctype").val();
    var act = $(form + " #payid_" + acctype + " .active").attr("payid");
    var account = $(form + " #account_" + acctype).val();
    var fee = $(form + " #fee_" + acctype).attr("fee");

    var valicode = $(form + " #valicode_" + acctype).val();
    var game = $(form + " #game").val();
    var paygame = $(form + " #paygame").val();
    var zone = $(form + " #zone").val();
    var charid = $(form + " #charid").val();
    var bankid = $(form + " #mod_bank label[class=active]").attr("bank");
    var pay_seq = Math.random() * 10000000000;
    var payclient = "pc";
    pay_seq = pay_seq.toString();
    Cookies.set("pay-seq", pay_seq);
    $("#orderresult").attr("seq", pay_seq);
    if (act == "qfillfrombank" || act == "qfillalipay") {
        var parameter = {
            "bank": bankid,
            "act": act,
            "pay-seq": pay_seq,
            "account": account,
            "fee": fee,
            "acctype": acctype,
            "game": game,
            "paygame": paygame,
            "zone": zone,
            "valicode": valicode,
            "charid": charid,
            "payclient": payclient
        };
        showPostPage("/action.php", parameter);
        var ebankPop = $("#pop-ebank");
        ebankPop.show();
        goCenter(ebankPop);
        clickflag = true;
        return false;
    }
    var param = 'pay-seq=' + pay_seq + '&bank=' + bankid + '&act=' + act + '&account=' + account + '&fee=' + fee + '&acctype=' + acctype + '&game=' + game + '&paygame=' + paygame + '&zone=' + zone + '&valicode=' + valicode + '&charid=' + charid + '&payclient=' + payclient;
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: param,
        beforeSend: function(request) {
            $(form + " #btn-ebank").addLoading();
        },
        success: function(result) {
            eval("var arr = " + result + "");
            switch (arr[0]) {
                case -1:
                    optionTips($(form + " #btn-ebank"), decodeURI(arr[1]));
                    break;
                case -2:
                    optionTips($(form + " #valicode_" + acctype), decodeURI(arr[1]));
                    changeimg(acctype);
                    $(form + " #valicode_" + acctype).focus();
                case -3:
                    optionTips($(form + " #account_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #account_" + acctype).val('').focus();
                    break;
                case -6:
                    var fatiguePop = $("#pop-fatigue");
                    fatiguePop.find(".fatigue").html("<p>" + arr[1] + "</p>");
                    fatiguePop.show();
                    goCenter(fatiguePop);
                    resizePop(fatiguePop);
                    break;
                case 1:
                    if (act == "qfillfromwxpay") {
                        var wechatPop = $("#pop-wechat");
                        wechatPop.show();
                        goCenter(wechatPop);
                        $("#wxqrcode").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-wechat .qr-success").hide();
                        $("#pop-wechat .qr-start").show();

                    } else if (act == "qfillalipayqrc") {
                        var alipayPop = $("#pop-alipay");
                        alipayPop.show();
                        goCenter(alipayPop);
                        $("#alipay_qrc").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-alipay .qr-success").hide();
                        $("#pop-alipay .qr-start").show();
                    } else if (act == "qfillfromunionpayqrc") {
                        var wechatPop = $("#pop-unionpayqrc");
                        wechatPop.show();
                        goCenter(wechatPop);
                        $("#unionpayqrcode").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-unionpayqrc .qr-success").hide();
                        $("#pop-unionpayqrc .qr-start").show();
                    }
                    break;
            }
            changeimg(acctype);
            $(form + " #valicode_" + acctype).val('');

            clickflag = true;
        },
        complete: function() {
            $(form + " #btn-ebank").removeLoading();
        },
        error: function() {
            clickflag = true;
            $(form + " #btn-ebank").removeLoading();
        }
    })
}

// zzb
function submitzzb() {
    var form = "#pay-ebank";
    if (clickflag == false) {
        // return false;
    }
    clickflag = false;
    var acctype = $(form + " #acctype").val();
    var act = $(form + " #payid_" + acctype + " .active").attr("payid");
    var account = $(form + " #account_" + acctype).val();
    var buy_number = $(form + " #buy_number_" + acctype).attr("fee");
    if (buy_number > limit) {
        return false;
    }

    var valicode = $(form + " #valicode_" + acctype).val();
    var game = $(form + " #game").val();
    var paygame = $(form + " #paygame").val();
    var zone = $(form + " #zone").val();
    var charid = $(form + " #charid").val();
    var charname = $(form + " #charname").val();
    var bankid = $(form + " #mod_bank label[class=active]").attr("bank");
    var pay_seq = Math.random() * 10000000000;
    var payclient = "pc";
    pay_seq = pay_seq.toString();
    Cookies.set("pay-seq", pay_seq);
    $("#orderresult").attr("seq", pay_seq);
    if (act == "qfillfrombank" || act == "qfillalipay") {
        var parameter = {
            "bank": bankid,
            "act": act,
            "pay-seq": pay_seq,
            "account": account,
            "buy_number": buy_number,
            "acctype": acctype,
            "game": game,
            "paygame": paygame,
            "zone": zone,
            "valicode": valicode,
            "charid": charid,
            "itemid": itemid,
            "charname": charname,
            "payclient": payclient,
            "paytype": "gamefill"
        };
        showPostPage("/action.php", parameter);
        var ebankPop = $("#pop-ebank");
        ebankPop.show();
        goCenter(ebankPop);
        clickflag = true;
        return false;
    }
    var param = 'paytype=gamefill&pay-seq=' + pay_seq + '&bank=' + bankid + '&act=' + act + '&account=' + account + '&buy_number=' + buy_number + '&acctype=' + acctype + '&game=' + game + '&paygame=' + paygame + '&zone=' + zone + '&valicode=' + valicode + '&charid=' + charid + '&itemid=' + itemid + '&charname=' + charname + '&payclient=' + payclient;
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: param,
        beforeSend: function(request) {
            $(form + " #btn-ebank").addLoading();
        },
        success: function(result) {
            eval("var arr = " + result + "");
            switch (arr[0]) {
                case -1:
                    optionTips($(form + " #btn-ebank"), decodeURI(arr[1]));
                    break;
                case -2:
                    optionTips($(form + " #valicode_" + acctype), decodeURI(arr[1]));
                    changeimg(acctype);
                    $(form + " #valicode_" + acctype).focus();
                case -3:
                    optionTips($(form + " #account_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #account_" + acctype).val('').focus();
                    break;
                case -6:
                    var fatiguePop = $("#pop-fatigue");
                    fatiguePop.find(".fatigue").html("<p>" + arr[1] + "</p>");
                    fatiguePop.show();
                    goCenter(fatiguePop);
                    resizePop(fatiguePop);
                    break;
                case -7:
                    var html = '';
                    limit = arr[1];
                    if (arr[1] > 0) {
                        html = '最多购买' + limit + '份！';
                    } else {
                        html = '今日已卖完！';
                    }
                    optionTips($(form + " #btn-ebank"), html);
                    break;
                case 1:
                    if (act == "qfillfromwxpay") {
                        var wechatPop = $("#pop-wechat");
                        wechatPop.show();
                        goCenter(wechatPop);
                        $("#wxqrcode").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-wechat .qr-success").hide();
                        $("#pop-wechat .qr-start").show();

                    } else if (act == "qfillalipayqrc") {
                        var alipayPop = $("#pop-alipay");
                        alipayPop.show();
                        goCenter(alipayPop);
                        $("#alipay_qrc").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-alipay .qr-success").hide();
                        $("#pop-alipay .qr-start").show();
                    } else if (act == "qfillfromunionpayqrc") {
                        var wechatPop = $("#pop-unionpayqrc");
                        wechatPop.show();
                        goCenter(wechatPop);
                        $("#unionpayqrcode").attr("src", "data:image/png;base64," + arr.img).attr("orderid", arr.payorderid);
                        $("#pop-unionpayqrc .qr-success").hide();
                        $("#pop-unionpayqrc .qr-start").show();
                    }
                    break;
            }
            changeimg(acctype);
            $(form + " #valicode_" + acctype).val('');

            clickflag = true;
        },
        complete: function() {
            $(form + " #btn-ebank").removeLoading();
        },
        error: function() {
            clickflag = true;
            $(form + " #btn-ebank").removeLoading();
        }
    })
}

//巨人一卡通消耗

function submitecard() {
    var form = "#pay-giantcard";
    if (clickflag == false) {
        return false;
    }
    clickflag = false;
    var acctype = $(form + " #acctype").val();
    var account = $(form + " #account_" + acctype).val();
    var valicode = $(form + " #valicode_" + acctype).val();
    var cardno = $(form + " #cardno_" + acctype).val().replace(/\s/g, "");
    var cardpwd = $(form + " #cardpwd_" + acctype).val().replace(/\s/g, "");
    var act = $(form + " #act").val();
    var param = 'payclient=pc&act=' + act + '&account=' + account + '&cardcode=' + cardno + '&acctype=' + acctype + '&cardpassword=' + cardpwd + '&valicode=' + valicode;
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: param,
        success: function(result) {
            eval("var arr = " + result + "");
            switch (arr[0]) {
                case 1:
                    $(form + " .results").html('<p>' + decodeURI(arr[1]) + '</p>').show();
                    break;
                case -1:
                    optionTips($(form + " #btn-trader"), decodeURI(arr[1]));
                    break;
                case -2:
                    optionTips($(form + " #valicode_" + acctype), decodeURI(arr[1]));
                    changeimg(acctype);
                    $(form + " #valicode_" + acctype).focus();
                case -3:
                    optionTips($(form + " #account_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #account_" + acctype).val('').focus();
                    break;
                case -4:
                    optionTips($(form + " #cardno_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardno_" + acctype).focus();
                    break;
                case -5:
                    optionTips($(form + " #cardpwd_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardpwd_" + acctype).val('').focus();
                    break;
                case -6:
                    var fatiguePop = $("#pop-fatigue");
                    fatiguePop.find(".fatigue").html("<p>" + arr[1] + "</p>");
                    fatiguePop.show();
                    goCenter(fatiguePop);
                    resizePop(fatiguePop);
                    break;
            }
            changeimg(acctype);
            $(form + " #valicode_" + acctype).val('');
            clickflag = true;
        },
        error: function() {
            clickflag = true;
        }
    })
}
//手机充值卡
function submitphonecard() {
    var form = "#pay-phonecard";
    if (clickflag == false) {
        //return false;
    }
    clickflag = false;
    var acctype = $(form + " #acctype").val();
    var account = $(form + " #account_" + acctype).val();
    var valicode = $(form + " #valicode_" + acctype).val();
    var cardno = $(form + " #cardno_" + acctype).val().replace(/\s/g, "");
    var cardpwd = $(form + " #cardpwd_" + acctype).val().replace(/\s/g, "");
    var fee = $(form + " #fee_" + acctype).attr("fee");
    var act = $(form + " #act").val();
    var param = 'fee=' + fee + '&act=' + act + '&account=' + account + '&cardno=' + cardno + '&acctype=' + acctype + '&cardpwd=' + cardpwd + '&valicode=' + valicode + '&payclient=pc';
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: param,
        success: function(result) {
            eval("var arr = " + result + "");
            switch (arr[0]) {
                case 1:
                    $(form + " .results").html('<p>充值订单生成！您的订单号是：' + arr[1] + '，</p><p>充值服务商反馈充值结果会有一定时间，您可以<a href="/p.php?order_id=' + arr[1] + '"  target="_blank" class="b-link">点击这里查询充值记录>></a></p>').show();
                    break;
                case -1:
                    optionTips($(form + " #btn-card"), decodeURI(arr[1]));
                    break;
                case -2:
                    optionTips($(form + " #valicode_" + acctype), decodeURI(arr[1]));
                    changeimg(acctype);
                    $(form + " #valicode_" + acctype).focus();
                case -3:
                    optionTips($(form + " #account_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #account_" + acctype).val('').focus();
                    break;
                case -4:
                    optionTips($(form + " #cardno_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardno_" + acctype).focus();
                    break;
                case -5:
                    optionTips($(form + " #cardpwd_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardpwd_" + acctype).val('').focus();
                    break;
                case -6:
                    var fatiguePop = $("#pop-fatigue");
                    fatiguePop.find(".fatigue").html("<p>" + arr[1] + "</p>");
                    fatiguePop.show();
                    goCenter(fatiguePop);
                    resizePop(fatiguePop);
                    break;
            }
            changeimg(acctype);
            $(form + " #valicode_" + acctype).val('');
            clickflag = true;
        },
        error: function() {
            clickflag = true;
        }
    })
}


//手机短贷支付
function submitphonesms() {
    var form = "#pay-phonesms";
    if (clickflag == false) {
        //return false;
    }
    clickflag = false;
    var acctype = $(form + " #acctype").val();
    var account = $(form + " #account_" + acctype).val();
    var valicode = $(form + " #valicode_" + acctype).val();
    var cellphone = $(form + " #cellphone_" + acctype).val().replace(/\s/g, "");
    var fee = $(form + " #fee_" + acctype).attr("fee");
    var act = $(form + " #act").val();
    var gateway = gettelphoneproductor(cellphone);
    if (gateway != "dianxin") {
        optionTips($(form + " #btn-sms"), "移动/联通充值功能即将开放，敬请期待!");
        clickflag = true;
        return false;
    }
    var param = 'fee=' + fee + '&act=' + act + '&account=' + account + '&cellphone=' + cellphone + '&acctype=' + acctype + '&valicode=' + valicode;
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: param,
        success: function(result) {
            eval("var arr = " + result + "");
            switch (arr[0]) {
                case 1:
                    var msg = "<p>支付提交成功！</p>";
                    for (data in arr[2]) {
                        msg += "<p>请尽快用手机<span>" + arr[2][data][0] + "</span>编辑<span>" + arr[2][data][1] + "</span>发送到<span>" + arr[2][data][2] + "</span>(信息费0.1元/条)</p>";
                    }
                    $(form + " .results").html(msg + '<p><a href="/p.php?order_id=' + arr[1] + '"  class="b-link">点击查询订单</a></p>').show();
                    break;
                case -1:
                    optionTips($(form + " #btn-sms"), decodeURI(arr[1]));
                    break;
                case -2:
                    optionTips($(form + " #valicode_" + acctype), decodeURI(arr[1]));
                    changeimg(acctype);
                    $(form + " #valicode_" + acctype).focus();
                case -3:
                    optionTips($(form + " #account_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #account_" + acctype).val('').focus();
                    break;
                case -4:
                    optionTips($(form + " #cardno_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardno_" + acctype).focus();
                    break;
                case -5:
                    optionTips($(form + " #cardpwd_" + acctype).parent(), decodeURI(arr[1]));
                    $(form + " #cardpwd_" + acctype).val('').focus();
                    break;
                case -6:
                    var fatiguePop = $("#pop-fatigue");
                    fatiguePop.find(".fatigue").html("<p>" + arr[1] + "</p>");
                    fatiguePop.show();
                    goCenter(fatiguePop);
                    resizePop(fatiguePop);
                    break;
            }
            changeimg(acctype);
            $(form + " #valicode_" + acctype).val('');
            clickflag = true;
        },
        error: function() {
            clickflag = true;
        }
    })
}

var yidong = {
    check: /^(134|135|136|137|138|139|147|150|151|152|157|158|159|178|182|183|184|187|188)[0-9]{8}$/,
    range: [30, 1, 2, 3, 4, 5, 10, 15, 20, 25],
    msg: '请用正常手机号充值，勿相信淘宝等购买手机号，否则风险自担'
}
var liantong = {
    check: eval(/^(130|131|132|155|156|185|186|145|176)[0-9]{8}$/),
    range: [30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 18, 20, 23, 25, 28],
    msg: '请用正常手机号充值，勿相信淘宝等购买手机号，否则风险自担'
}
var dianxin = {
    check: eval(/^(133|153|180|181|189|177)[0-9]{8}$/),
    range: [1, 2, 3, 4, 5, 10, 15, 20, 25, 30],
    msg: '请用正常手机号充值，勿相信淘宝等购买手机号，否则风险自担'
}
//手机检查
var Gate;

function gettelphoneproductor(phonenum) {
    if (phonenum.match(yidong.check) !== null) {
        return 'yidong';
    } else if (phonenum.match(liantong.check) !== null) {
        return 'liantong';
    } else if (phonenum.match(dianxin.check) !== null) {
        return 'dianxin';
    }
    return '';
}

window.setInterval("resetwximg()", 295000);

function resetwximg() //重新生成微信二维码
{
    var $orderid = $("#wxqrcode").attr("orderid");
    var $orderidalipay = $("#alipay_qrc").attr("orderid");
    if ($orderid == "" && $orderidalipay == "") {
        return false;
    }
    var form = "#pay-ebank";
    var acctype = $(form + " #acctype").val();
    var act = $(form + " #payid_" + acctype + " .active").attr("payid");
    submitbank();
}

//循环调取
var payorderobject = {};
var exehandle = window.setInterval("reselectorder()", 10000);

function reselectorder() {
    var orderids = "";
    $(".orders").each(function() {
        var orderid = $(this).attr("orderid");
        var pop = $(this).attr("parentpop");
        if ($(pop).is(':hidden') || orderid == "") {

        } else if (!(typeof payorderobject[orderid] == "undefined") || payorderobject[orderid] == "") {
            if (payorderobject[orderid].result == "-1") {
                payorderobject[orderid] = {
                    "payorderid": orderid,
                    "result": "-1",
                    "pop": pop
                };
                orderids += "orderids[]=" + orderid;
            }
        } else {
            payorderobject[orderid] = {
                "payorderid": orderid,
                "result": "-1",
                "pop": pop
            };
            orderids += "orderids[]=" + orderid;
        }
    })

    if (orderids == "") return false;
    var params = "act=orderflag&";
    params += orderids;
    $.ajax({
        type: "POST",
        url: "action.php",
        data: params,
        dataType: 'json',
        success: function(result) {
            if (result[0] == 1) {
                for (var key in result.data) {
                    if (result.data[key] == "1001") {
                        payorderobject[key].result = 1001;
                        $("#" + payorderobject[key].pop + " .qr-success").show();
                        $("#" + payorderobject[key].pop + " .qr-start").hide();
                    }
                }
            }
        }
    });
}

$("#orderresult").on('click', function() {
    var orderid = Cookies.get($(this).attr("seq"));
    if (typeof orderid == "undefined" || orderid == "") {
        var orderurl = "/m.php?t=query_order";
    } else {
        var orderurl = "/p.php?order_id=" + orderid;
    }
    $(this).attr("href", orderurl);
})

function showPostPage(gateway, parameter) {

    if ($("#submitdataform").length >= 1) {
        $("#submitdataform").remove();
    }
    var f = document.createElement('form');
    f.setAttribute('method', 'post');
    f.setAttribute('action', gateway);
    f.setAttribute('id', 'submitdataform');
    f.setAttribute('target', "_blank");
    for (paramkey in parameter) {
        var chrcodeinput = document.createElement('input');
        chrcodeinput.setAttribute('type', 'hidden');
        chrcodeinput.setAttribute('name', paramkey);
        chrcodeinput.setAttribute('value', parameter[paramkey]);
        f.appendChild(chrcodeinput);
    }
    document.body.appendChild(f);
    f.submit();
    try {
        window.opener = null;
    } catch (ex) {}
    //document.location.href = "pay://sdk.install.mobpay.ztgame.com/break";
}

var checkaccountlist = {};
$(".input-account").on("blur", function() {
    var id = $(this).attr("id");
    var $account = $(this).val();
    if (!checkAccount($(this)) || !$account.length) {
        $("#" + id).attr("isok", 0);
        return false;
    }
    var datakey = $account;
    var checkdata = "";
    if (!(typeof checkaccountlist[datakey] == undefined) || checkaccountlist[datakey] == "") {
        checkdata = checkaccountlist[datakey];
    }
    if (checkdata == 1) {
        $("#" + id).attr("isok", 1);
        return false;
    } else if (checkdata == -3) {
        $(this).attr("isok", 0);
        $(this).parent().addClass("has-error");
        optionTips($(this).parent(), "输入的账号不存在");
        return false;
    }
    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: "act=checkaccount&account=" + $account,
        dataType: 'json',
        beforeSend: function(request) {
            request.setRequestHeader("account-alis", $account);
        },
        success: function(result) {
            checkaccountlist[datakey] = result[0];
            switch (result[0]) {
                case 1:
                    $("#" + id).attr("isok", 1);
                    break;
                default:
                    $("#" + id).attr("isok", 0);
                    $("#" + id).parent().addClass("has-error");
                    optionTips($("#" + id).parent(), decodeURI(result[1]));
                    break;
            }
        },
        error: function() {
            $("#" + id).attr("isok", 0);
        }
    })
});